package net.zjitc.servlet.login;

import net.zjitc.domain.User;
import net.zjitc.service.UserService;
import net.zjitc.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name="gg" ,urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    UserService userService=new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        User user=userService.findByUsernameAndPassword(username,password);
        resp.setHeader("Content-Type","text/html;charset=Utf-8");
        PrintWriter writer = resp.getWriter();
        if(user!=null){
            HttpSession session = req.getSession();
            session.setAttribute("user",username);
            writer.write("登录成功");
        }
        else{
            writer.write("用户名和密码错误");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
